package com.kkw.shoppingmall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import com.kkw.shoppingmall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000117676119";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQChgWrqtQK5iJCLQtpxU0VJwFO1xwqa8HTJHk6wq6Orr/NSqNGa3tRmfAyNyVLi0ObSMaKgIawmuGp3EdicnLqmztf/N6D2yWrAQmeGHlAREMycA1OGQw7BeN8+AI41/lQpwDZ/y2aLmoe+StmVVAXpxPr8ehZB+eIaUeGzvFnKziKO1apX8/PJqSShEa1KMYHG64dd/Tx+CEMWQrEzb55Q+mxxgmdwm3HN4HAiQmkUP+tlFCAX5zn4XPCUDngQAdKTy9QJcjDZoGfLrrh8L7E04sfVj51trdSGEoI568e1pp39WQuizSuc7Xm2A0hkO/QjfEdoyaD3/yIDqwHOfYUnAgMBAAECggEAEvNmoke/HBokK9MaYrXMqYzBxdCnM+sHKRo/TYe2Wsz4ljhs7+k5rxTbPD/lU+wDSaK9XGDXHGwDQhl7+1SAqlYXDkFux8Z2mjywfRJNftMjzyKsyFJSBu2x9EqIZVmEcqm1O2WYkMT/LJsH0PfdAcM1WC7TMcaqe0AkZbyyJJO/M19BfhvkIryN/6Yq00agTGm07qKMZzNZn8rrldLqpuBPYD9xLOgyeI3lUefxYtZwJZQjp7QEy0byBGvKItOMkvHw1PKK5me934f43OmAaJcj6q3Mi6tNSmvn64saLRZWUGIUnzrfTu9dJZYREG/VB3DvwCNMtlgC25VfwEunEQKBgQDuZwFmhxRISjuYfO2nw5D2cDo9oGunBRSwz1MQOHrmpM4NscwWdKdxjcZy7ZolqjgBcfWwkL4wFiZZs2/cusWmES8YK9m6Rn/IyEXZ4bZszhNyqQV4QHBBO/RMXZsNJqq8U60PpGcVZJQ0Tm59TM6Ci1wDZ/ochaimdd0JTEWmuwKBgQCtbVLN/ZHDYQtiUnmPoZw7Ez8xGDOJ1E6xIdU3oEHeKaUBL3kD8IWy3AUFRLpDdGxZq5o7u+cKkqiqEtPJHpVYK5M4aDxjXj3XKhkG6+ONK6KkAryjfyOTw8lPPvPs9AkEhtZcweT4ZhEYf4J2UYWhSil7B28DwmjRlrf0dVJShQKBgQDqferEvCzZBhskXA0urI/TtoOyxi3dNIQrfWHIXKWWY5hBpBaIJ+njKX5+4PJ0Bb/cvAjbySMpDUBfjr2CsTtjoxddgRRBwsJB7au3KepTj0dVyqaUp11C7QPYcWHnR5c8clmRPzYdtraH1HWdTYT6prS0rPs4PseqI9hsIteHrQKBgGmwPQ1B4Yni4zApOOG6SF6MDS+bIxV05/v0tV6v8FQ2ptMBHkuzUZc9lGWwi56+HaXL+TM8AMIlvFnhHWXsmHDiJVbz1xFrhl5Qzm20ZblLhVNUQ6eTvJe5cj40DC00TyaOEab3ltV2Riba1iPK7JftfTf3wMTWvlAz/KkismeNAoGBAMuS/eZRYOwCyRoZ9O5yyDPeZwcY3vr+wuEn66KmUJuRgshkajqe+SISxwG+hg46eH4weo7A6qMsgUYan2M3EJ/9Hm6Sq+Ke61Wp/sN6XspY8V2kU0cPC+sII5F4LxR8HkwH85fXZiEK+JkE/XhjQzcYC6bEftqGemHIbYlDc4np";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsYxc0boUILJfnA3FHumHFCCiAgoqADjl4eMtOm/gMIdav5VChf+4WQnJTqXxVKNKD7DypHNkAES4yubkFlOwqfNBx6jKpDLixqCw7xJ1xtO/Ol9M0ptDZBqKf0RDCZLD4Hn4h/DHXPSfIX3zgFbFPPwaSnd9TG15bC4wzqZtzZeF0ePyVl8I8rOSn3F0DItwws3leZobElY5k8tPZiYsyjo/n9QXlN7r6/X5MO/0Q3ICQufl1N1Py66Dx9cGUdVMxqKTd9Ue3gjadnCpcIHEPMvYhxLWkEZ18Jsekz9OCOAIuRYGfNMxt742WYte8sCVQx67dELlYUo3Emw/3cr0lQIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://shoppingmall.free.idcfengye.com/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.shoppingmall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"1m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
